﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Joyeon.Utils
{
    /// <summary>
    /// Ini文件读写工具,强烈建议ini文件第一行为";"，否则有可能读不出来值。
    /// </summary>
    public static class IniUtil
    {
        private static INIClass ini = null;

        /// <summary>
        /// 加载指定路径的ini文件
        /// </summary>
        /// <param name="path"></param>
        public static void Load(string path)
        {
            if (ini == null)
            {
                ini = new INIClass(path);
            }
        }

        /// <summary>
        /// 重新加载指定路径的ini文件
        /// </summary>
        /// <param name="path"></param>
        public static void ReLoad(string path)
        {
            ini = new INIClass(path);
        }
        /// <summary>
        /// 从文件中读出一个值
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string ReadValue(string section, string key)
        {
            return ini.IniReadValue(section, key);
        }
        /// <summary>
        /// 向文件中写入一个值
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void WriteValue(string section, string key, string value)
        {
            ini.IniWriteValue(section, key, value);
        }
        private class INIClass
        {
            private string inipath;

            public string Inipath
            {
                get { return inipath; }
                set { inipath = value; }
            }
            [System.Runtime.InteropServices.DllImport("kernel32")]
            private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
            [System.Runtime.InteropServices.DllImport("kernel32")]
            private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
            /// <summary>
            /// 构造方法
            /// </summary>
            /// <param name="INIPath">文件路径</param>
            public INIClass(string INIPath)
            {

                inipath = INIPath;
            }
            /// <summary>
            /// 写入INI文件
            /// </summary>
            /// <param name="Section">项目名称(如 [TypeName] )</param>
            /// <param name="Key">键</param>
            /// <param name="Value">值</param>
            public void IniWriteValue(string Section, string Key, string Value)
            {
                WritePrivateProfileString(Section, Key, Value, this.inipath);
            }
            /// <summary>
            /// 读出INI文件
            /// </summary>
            /// <param name="Section">项目名称(如 [TypeName] )</param>
            /// <param name="Key">键</param>
            public string IniReadValue(string Section, string Key)
            {
                StringBuilder temp = new StringBuilder(500);
                int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
                string s = temp.ToString();
                return temp.ToString();
            }
            /// <summary>
            /// 验证文件是否存在
            /// </summary>
            /// <returns>布尔值</returns>
            public bool ExistINIFile()
            {
                return System.IO.File.Exists(inipath);
            }
        }
    }
}
